const services = {};

async function internalAdd(clsName, modName, isSingle) {
  const modClass = require('./' + modName);
  services[clsName] = {
    'clsName': clsName,
    'modName': modName,
    'class': modClass,
    'isSingle': isSingle
  };
  if (modClass.staticInit) {
    await modClass.staticInit();
  }
}

async function add(clsName, modName) {
  await internalAdd(clsName, modName, false);
}

async function addSingle(clsName, modName) {
  await internalAdd(clsName, modName, true);
}

async function init() {
  await add("BlockChain", 'blockchain');
}

function create(name, session = null) {
  const service = services[name];
  return new service['class'](session);
}

exports.init = init;
exports.create = create;
